var groupList = {
	groupId:null
}

$(function(){
	changeActiveItem('groupList');
	initTable();
	savehandle();
});

function savehandle(){
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		successMsg:'删除成功',
		failmsg:'删除失败',
		fieldMap:{},
		success:successCallBack
	});
}
function successCallBack(){
	refresh();
}

/**
 * 获取数据
 */
function initTable(){
	$('#grid').grid({
		title:false,
		pager:true,
		checkbox:true,
		multiSelect:true,
		tbar:'#navbar',
		store:{
			url:'/fw/auth/group_getAllGroups.action',
			idProperty:'groupId',
			uploadpagesize:'rows'
		},
		pageCfg:{
			btnCount:5
		},
		columns:[
		    {cn:"组ID",en:"groupId",width:100,hidden:true},
			{cn:'组名称',en:'groupName',width:300},
			{cn:'备注',en:'remarks',width:200},
			{cn:'登录后默认url',en:'loginUrl',width:200}
		]
	}) ;
}

/**
 * 刷新
 */
function refresh(){
	$('#grid').grid('load',{data:{}});
}

/**
 * 判定单条删除或批量删除
 * @return {} 
 */
function del(){
	var ids = $('#grid').grid('getSelectedIds');
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要删除的数据!',timer:2000});
		return;
	}else{
		var id  = ids.join(",");
	}
	if(confirm("确定要删除这些数据吗？")){
		if(confirm("请再次确认，删除数据后不能恢复！")){
		$.getJSON(
			'/fw/auth/group_delBatch.action',
			{
				'ids':id
			},
			function(data){
				$('#alertdiv').savehandle('handle',data,2000);
			}
		);
	}
}
}

/**
 * 添加组信息
 */
function add(){
	window.location.href = "addGroup.jsp";
}

/**
 * 编辑组信息
 * @return {} 
 */
function edit(){
	var checked = $('#grid').grid('getSelectedIds');
	if(checked.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要修改的数据!',timer:2000});
		return;
	}else if(checked.length>1){
		$('#alertdiv').savehandle('setmsg',{msg:'只能修改一条数据!',timer:2000});
		return;
	}else{
		groupList.groupId = checked[0];
		location.href = "editGroup.jsp#"+groupList.groupId+',admin';
	}
}

/**
 * 设置组权限
 * @return {} 
 */
function groupAuth(){
	var checked = $('#grid').grid('getSelectedIds');
	if(checked.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要修改的数据!',timer:2000});
		return;
	}else if(checked.length>1){
		$('#alertdiv').savehandle('setmsg',{msg:'只能修改一条数据!',timer:2000});
	}else{
		groupList.groupId = checked[0];
		location.href = "groupAuth.jsp#"+groupList.groupId+',admin';
	}
}
/**
 * 设置组菜单
 * @return {} 
 */
function groupMenu(){
	var checked = $('#grid').grid('getSelectedIds');
	if(checked.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要修改的数据!',timer:2000});
		return;
	}else if(checked.length>1){
		$('#alertdiv').savehandle('setmsg',{msg:'只能修改一条数据!',timer:2000});
	}else{
		groupList.groupId = checked[0];
		location.href = "groupMenu.jsp#"+groupList.groupId+',admin';
	}
}